package cn.liuweiwei.controller;


import cn.liuweiwei.domain.Resource;
import cn.liuweiwei.service.ResourceService;
import cn.liuweiwei.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("resources")
@CrossOrigin
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("{page}/{limit}")
    public Result findResources(@PathVariable("page") String page,
                                @PathVariable("limit") String limit) {
        return Result.successWithObj(resourceService.findResources(page, limit));
    }

    @GetMapping("{title}")
    public Result findResources(@PathVariable("title") String title) {
        return Result.successWithObj(resourceService.findResources(title));
    }

    @DeleteMapping("{id}")
    public Result deleteResource(@PathVariable("id") String id) {
        resourceService.deleteResource(id);
        return Result.success("删除成功");
    }

    @PutMapping
    public Result updateResource(@RequestBody Resource resource) {
        resourceService.updateResource(resource);
        return Result.success("更新成功");
    }

    @PostMapping
    public Result insertResource(@RequestBody Resource resource) {
        resourceService.insertResource(resource);
        return Result.success("添加成功");
    }
}